<?php
class person_parser
{
    public static function get_person($id, $lang='en', $xml=true)
    {
        //cleanup using regex patterns for ids
        $patterns[0] = '/title/';
        $patterns[1] = '/name/';
        $patterns[2] = '/genre/';
        $patterns[3] = '/\//';

        //XPATHS
        $movieXpath['title'] = '//h1[@itemprop="name"]';
        $movieXpath['description'] = '//span[@itemprop="description"]';
        $movieXpath['images'] = '//div[@class="mediastrip"]//noscript/img';
        $movieXpath['birthdate'] = '//time[@itemprop="birthDate"]';
        $movieXpath['knownfor'] = '//div[@id="knownfor"]/div';
        $movieXpath['filmography'] = '//*[@id="filmography"]/div[2]/div';
        $movieXpath['poster'] = '//td[@id="img_primary"]//img';

        //get movie url
        $imdburl = 'http://www.imdb.com/name/'.$id;



        //prepare DOMDocument
        $html = new DOMDocument();
        $contents = udnutil::read_url($imdburl);
        $contents = str_replace('<div class="clear"/>&nbsp;</div>',"", $contents);
        //file_put_contents("aa.html", $contents);
        @$html->loadHTML($contents);
        $xpath = new DOMXPath( $html );

        //DATA INFO
        $movie['title'] = person_parser::clean(person_parser::getFirstNode($xpath, $movieXpath['title']));
        $movie['poster'] = (person_parser::clean(person_parser::parseAttribute($xpath, $movieXpath['poster'], "src")));
        $movie['large_poster'] = (udnutil::originalImdbImage(person_parser::clean(person_parser::parseAttribute($xpath, $movieXpath['poster'], "src"))));

        $movie['description'] = person_parser::clean(person_parser::getFirstNode($xpath, $movieXpath['description']));
        $movie['birthdate'] = person_parser::clean(person_parser::parseAttribute($xpath, $movieXpath['birthdate'],'datetime'));

        //DATA IMAGES
        $imagesList = person_parser::parse($xpath, $movieXpath['images']);
        $i = 0;
        foreach($imagesList as $image)
        {
            $images[$i]['thumbnail'] = ($image->getAttribute('src'));
            $images[$i]['original'] = (udnutil::originalImdbImage($image->getAttribute('src')));
            $i++;
        }
        $movie['images'] = $images;

        //DATA MOVIES
        $nodelist = person_parser::parse($xpath, $movieXpath['knownfor']);
        $length = $nodelist->length;

        for($i = 0; $i < $length; $i++)
        {

            $tmpNode = $xpath->query('.//noscript/img', $nodelist->item($i))->item(0);
            if($tmpNode!=null)
                $knownfor['img'] = ($tmpNode->getAttribute('src'));


            $knownfor['title'] = htmlspecialchars($xpath->query('./a[2]', $nodelist->item($i))->item(0)->nodeValue);
            $knownfor['movieid'] = preg_replace($patterns,'',$xpath->query('./a[2]', $nodelist->item($i))->item(0)->getAttribute('href'));
            $knownfors[$i] = $knownfor;

        }
        $movie['knownfors'] = $knownfors;

        //ACTOR MOVIES
        include('search_parser.php');
        $filmography = search_parser::get_search("count=50&role=".$id);
        $movie['filmography'] = $filmography['results'];

        if(false && $lang == 'ar')
        {
            //arabic translation
            $movie['title_lang'] = lang::translate($movie['title']);
            $movie['description'] = lang::translate($movie['description']);
        }

        if($xml)
            return person_parser::TestBlockHTML($movie, $id);
        else
            return $movie;
    }

//STATIC FUNCTIONS
    public static function parse($xpath, $tag)
    {
        $nodelist = $xpath->query( $tag );
        return $nodelist;
    }

    public static function getFirstNode($xpath, $tag)
    {
        $nodelist = person_parser::parse($xpath, $tag);
        return $nodelist->item(0)->nodeValue;
    }

    public static function parseAttribute($xpath, $tag, $attribute)
    {
        $nodelist = person_parser::parse($xpath, $tag);
        if($nodelist->length > 0)
        {
            //echo $nodelist->item(0)->nodeValue.$nodelist->item(0)->getAttribute($attribute)."<br />";
            return $nodelist->item(0)->getAttribute($attribute);
        }
        else
            return "";
    }
    public static function clean($value)
    {
        return trim(preg_replace("/[\n\r]/","",$value));
    }


    public static function TestBlockHTML ($movie, $id)
    {
        ob_start(); ?>
    <movie>
        <mode>person</mode>
        <imdb><?echo $id?></imdb>
        <trailer><?echo $movie['trailer']?></trailer>
        <title><?echo $movie['title']?></title>
        <year><?echo $movie['year']?></year>
        <poster scaled="<?echo udnutil::originalImdbImage($movie['poster'])?>"><?echo $movie['poster']?></poster>
        <rating><?echo $movie['rating']?></rating>
        <votes><?echo $movie['votes']?></votes>
        <release><?echo $movie['release']?></release>
        <runtime><?echo $movie['runtime']?></runtime>
        <description><?echo $movie['description']?></description>
        <images><?foreach($movie['images'] as $image) {?><image scaled="<?echo udnutil::dlurl(udnutil::originalImdbImage($image))?>"><?echo $image?></image><?}?></images>
        <knownfors><?foreach($movie['knownfors'] as $mov) {?>
            <movie id="<?echo $mov['movieid']?>" title="<?echo htmlspecialchars($mov['title'])?>" poster="<?echo $mov['img']?>"/><?}?>
        </knownfors>
        <filmography><?foreach($movie['filmography'] as $mov) {?>
            <movie id="<?echo $mov['movieid']?>" title="<?echo htmlspecialchars($mov['title'])?>" year="<?echo $mov['year']?>"/><?}?>
        </filmography>
        <movies><?
            $fullurl = "http://127.0.0.1".$_SERVER['REQUEST_URI'];
            $segs = explode('/', $fullurl);
            $baseUrl = str_replace($segs[count($segs) - 1], "", $fullurl);
            echo udnutil::read_url($baseUrl.'/search_parser?person='.$id.'&type=movies'); ?></movies>
        <genres></genres>
        <directors></directors>
        <cast></cast>
    </movie>
    <?php return ob_get_clean();
    }
}
?>
